package deibay.client.gui;

import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.swingx.JXList;

import deibay.client.core.views.IPanelBidder;
import deibay.client.core.views.IViewItemDetailsBidder;
import deibay.util.Pair;

public class PanelBidderView extends JSplitPane implements IPanelBidder{

	private JXList itemsList = null;	
	private JScrollPane scrollList;
	private JScrollPane scrollDetails;
	private ItemDetailsView itemDetails = null;
	
	public PanelBidderView()
	{
		super();
		initialize();
	}
	
	private void initialize()
	{		
		this.setDividerLocation(225);
		this.setDividerSize(13);		
		setSize(new Dimension(750, 600));
		setLeftComponent(getScrollListComponent());
		itemDetails = new ItemDetailsView();
		setRightComponent(itemDetails);
		getItemsComponent().setModel(new DefaultListModel());
		getItemsComponent().setCellRenderer(new ListItemsRenderer());
	}
	
	private JScrollPane getScrollListComponent() {
		if(scrollList == null)
		{
			scrollList = new JScrollPane();
			scrollList.setSize(300, 600);
			scrollList.setViewportView(getItemsComponent());
		}
		return scrollList;
	}

	private JScrollPane getScrollDetailsComponent() {
		if(scrollDetails == null)
		{
			scrollDetails = new JScrollPane();
			scrollDetails.setSize(300, 600);	
			scrollDetails.setViewportView(new ItemDetailsView());
		}
		return scrollDetails;
	}
	
	private JXList getItemsComponent()
	{
		if(itemsList == null)
		{
			itemsList = new JXList();			
		}
		return itemsList;
	}
	
	public IViewItemDetailsBidder showItemDetailsView() {				
		return itemDetails;
	}
	
	public void setItems(Vector<Pair<String, String>> items) {
		Vector<ItemObjectIcon> objectItems = new Vector<ItemObjectIcon>();
		for(Pair<String,String> item: items)
		{
			ItemObjectIcon itemObj = new ItemObjectIcon(item.getFirst(), item.getSecond());
			objectItems.add(itemObj);
		}
		itemsList.setListData(objectItems);
	}

	public void setChangeItemAction(ListSelectionListener listener)
	{
		this.itemsList.addListSelectionListener(listener);
	}
	
	public void setClickItemAction(MouseListener listener)
	{
		this.itemsList.addMouseListener(listener);
	}
	
	public void setBidderViewEnabled(boolean b)
	{
		this.setEnabled(b);
	}
}
